/**************************
* @Summary
* @Author 20009200303 赵传博
* @Date 2022/4/3
*****************************/

#include "Parser.h"
#include "Error.h"
using namespace Lexer;
using namespace myError;

double Parser::prim(bool get, int argc) {
    if(get) get_token();
    switch (curr_tok) {
        case NUMBER:{
            double val = number_value;
            get_token();
            return val;
        }
        case NAME:{
            map<string, double>::iterator it;
            if(table.end() != (it = table.find(string_value))){
                double val = table.at(string_value);
                if(get_token() == ASSIGN) val = expr(true, argc);
                return val;
            }else{
                error("No match constant.");
                return -0.114514;
            }
        }
        case MINUS:
            return -prim(true, argc);
        case LP:{
            double e = expr(true, argc);
            if(curr_tok != RP) return myError::error(") expected");
            get_token();
            return e;
        }
        default:
            return myError::error("primary expected");
    }
}

double Parser::prim(bool get, ifstream *inStream, int argc) {
    if(get) get_token(inStream);
    switch (curr_tok) {
        case NUMBER:{
            double val = number_value;
            get_token(inStream);
            return val;
        }
        case NAME:{
            map<string, double>::iterator it;
            if(table.end() != (it = table.find(string_value))){
                double val = table.at(string_value);
                if(get_token(inStream) == ASSIGN) val = expr(true, argc);
                return val;
            }else{
                error("No match constant.");
                return -0.114514;
            }
        }
        case MINUS:
            return -prim(true, argc);
        case LP:{
            double e = expr(true, argc);
            if(curr_tok != RP) return myError::error(") expected");
            get_token(inStream);
            return e;
        }
        default:
            return myError::error("primary expected");
    }
}

double Parser::term(bool get, int argc) {
    double left;
    if(argc == 1){
        left = prim(get, argc);
    }else{
        left = prim(get, input, argc);
    }
    for(;;){
        switch (curr_tok) {
            case MUL:
                if(argc == 1) left *= prim(true, argc);
                else left *= prim(true, input, argc);
                break;
            case DIV:
                double d;
                if(argc == 1) d = prim(true, argc);
                else d = prim(true, input, argc);
                if(d){
                    left /= d;
                    break;
                }
                return myError::error("divide by 0");
            default:
                return left;
        }
    }
}


double Parser::expr(bool get, int argc) {
    double left = term(get, argc);
    for(;;){
        switch (curr_tok) {
            case PLUS:
                left += term(true, argc);
                break;
            case MINUS:
                left -= term(true, argc);
                break;
            default:
                return left;
        }
    }
}